<?php
/**
 * Created on 18 mars 08 by Eric Jeker <eric.jeker@gmail.com>
 * Created on Eclipse 3.2 with PHPEclipse 1.1.8
 * (c) 2007 - 2008 Copyright Eric Jeker <eric.jeker@gmail.com>
**/

class eveTrusted extends sfFilter
{
  public function execute($filterChain)
  {
		$browser = isset($HTTP_SERVER_VARS) ? $HTTP_SERVER_VARS['HTTP_USER_AGENT'] : $_SERVER['HTTP_USER_AGENT'] ;
		$ingame = (substr($browser,0,16)=='EVE-minibrowser/');

		$this->getContext()->getRequest()->setParameter('ingame', $ingame) ;

		$module = $this->getContext()->getModuleName() ;
		$action = $this->getContext()->getActionName() ;
		
		$need_ingame = false ;
		if ($module == 'player' && $action == 'subscribe')	{
			$need_ingame = true ;
		}

		if ($ingame)	{
		  // check for trust 
		  if ($_SERVER['HTTP_EVE_TRUSTED']=='no') {
		    // request trust from client through headers
		    header("eve.trustme:http://" . $_SERVER['HTTP_HOST'] . "/::please allow me to access your pilot information.");
		  } else {
		  	// get pilotname
		    $pilotname = $_SERVER['HTTP_EVE_CHARNAME'] ;
		    
		    // checking if user exists
		    $c = new Criteria() ;
		    $c->add(sfGuardUserPeer::USERNAME, $pilotname) ;
		    $user = sfGuardUserPeer::doSelectOne($c) ;
		    
		    if ($user)	{
        	$this->getContext()->getUser()->signIn($user) ;
		    }
		  }
		} else if ($need_ingame)	{
			die('You need to access this page from your ingame browser and trust it !') ;
		}
 
    // Execute next filter
    $filterChain->execute();
  }
}

?>